package com.spc.car.web.log.module;

import com.alibaba.fastjson.JSON;
import com.spc.car.common.utils.spring.SpringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.entity.log.LogProjectOperation;
import com.spc.car.data.entity.sys.SysAttachment;
import com.spc.car.data.service.sys.SysAttachmentService;
import com.spc.car.web.log.ProjectAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

public class LogSysAttachment extends BaseLogModule<LogProjectOperation, LogProjectAction> {
    public SysAttachmentService attachmentService;

    public LogSysAttachment() {
        this.attachmentService = SpringUtils.getBean(SysAttachmentService.class);
    }

    public LogProjectOperation toLogRecord(LogProjectAction logProjectAction,
                                           Map<String, Object> paramsMap,
                                           AjaxResult result) {
        LogProjectOperation logProjectOperation = new LogProjectOperation();

        ProjectAction[] projectActions = logProjectAction.action();
        SysAttachment attachment = null;
        if (StringUtils.isNotBlank(logProjectAction.idForPost())
                && Objects.nonNull(paramsMap.get(logProjectAction.idForPost()))) {
            attachment = attachmentService.getById((Long)paramsMap.get(logProjectAction.idForPost()));
        }

        for (ProjectAction projectAction: projectActions) {
            if (projectAction.equals(ProjectAction.UPLOAD_ATTACHMENT)) {
                if (!Objects.isNull(paramsMap.get("params"))) {
                    try {
                        Map<String, Object> paramMaps = JSON.parseObject((String) paramsMap.get("params"), Map.class);
                        if (MapUtils.isNotEmpty(paramMaps) && Objects.nonNull(paramMaps.get("projectCode"))) {
                            String projectCode = (String)paramMaps.get("projectCode");

                            if (projectCode.startsWith("PP")) {
                                logProjectOperation.setProjectCode(projectCode);
                            } else if (projectCode.startsWith("PT")) {
                                logProjectOperation.setProjectTenderingSectionCode(projectCode);
                            } else {
                                logProjectOperation.setProjectTenderingSectionCode(projectCode);
                            }
                            logProjectOperation.setRemark(projectAction.getName()
                                    + "[" + paramsMap.get("file") + "]");
                        }
                    } catch (Exception e) {

                    }
                }
                break;
            } else if (projectAction.equals(ProjectAction.REMOVE_ATTACHMENT)
                    && null != attachment && StringUtils.isNotBlank(attachment.getProjectCode())) {
                if (attachment.getProjectCode().startsWith("PP")) {
                    logProjectOperation.setProjectCode(attachment.getProjectCode());
                } else if (attachment.getProjectCode().startsWith("PP")) {
                    logProjectOperation.setProjectTenderingCode(attachment.getProjectCode());
                } else {
                    logProjectOperation.setProjectTenderingSectionCode(attachment.getProjectCode());
                }

                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + attachment.getFileName() + "]");
                break;
            }
        }

        return logProjectOperation;
    }
}
